package com.cjg.javaBasics;

import javax.sound.midi.Soundbank;

public class less {
    public static void main(String[] args) {
        //创建二维数组
        int [] [] a1 =new int[11][11];
        a1[1][1]=1;a1[0][3]=2;
        //设置标记，计算非0个数；
        int sum=0;
        for (int[] ints : a1) {
            for (int anInt : ints) {
                if (anInt!=0){
                    sum++;
                }
                System.out.print(anInt+" ");

            }   System.out.println();

        }
        System.out.println("=========================================");

        System.out.println(sum);
        System.out.println("=========================================");

        //创建稀疏数组
        int[][] less =new int[sum+1][3];
        less[0][0]=11; less[0][1]=11; less[0][2]=sum;
//        循环二维数组将非0赋值给稀疏数组
        int temp=0;
        for (int i = 0; i <a1.length ; i++) {
            for (int j = 0; j <a1[i].length ; j++) {
                if (a1[i][j]!=0){
                    temp++;
                    less[temp][0]=i;
                    less[temp][1]=j;
                    less[temp][2]=a1[i][j];
                }

            }
        }
        for (int[] ints : less) {
            for (int anInt : ints) {
                System.out.print(anInt+" ");
            }
            System.out.println();
        }
        System.out.println("=========================================");

        //稀疏数组转为二维数组
         int[][] re =new int[less[0][0]][less[0][1]];
        for (int[] ints : re) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    sum++;
                }
                System.out.print(anInt + " ");

            }  System.out.println();
        }
        System.out.println("=========================================");
        for (int i = 1; i <less.length ; i++) {
            re[less[i][0]][less[i][1]]=less[i][2];
        }
        for (int[] ints : re) {
            for (int anInt : ints) {
                System.out.print(anInt+" ");
            }
            System.out.println();
        }





        }
}
